package com.chensi.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chensi.common.AjaxJson;
import com.chensi.common.Constants;
import com.chensi.common.Pager;
import com.chensi.model.Ticket;
import com.chensi.service.ITicketService;
import com.chensi.util.BeanUtils;

@Controller
@RequestMapping("ticket")
public class TicketController {
	
	@Autowired
	private ITicketService ticketService;

	@RequestMapping("main")
	public String main() {
		return "ticket/main";
	}
	
	@RequestMapping("addView")
	public ModelAndView addView() {
		return new ModelAndView("ticket/edit");
	}
	
	@RequestMapping("updateView")
	public ModelAndView updateView(Ticket model) {
		Ticket entity = ticketService.get(model.getId());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ticket", entity);
		return new ModelAndView("ticket/edit", map);
	}
	
	//===================
	
	/** 角色list */
	@RequestMapping({ "list.do", "list.json" })
	@ResponseBody
	public AjaxJson list() {
		return AjaxJson.getSuccessInfo(ticketService.list());
	}
	
	/**
	 * 获取详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("get.do")
	@ResponseBody
	public AjaxJson get(Integer id) {
		if (id == null) {
			return AjaxJson.getFailInfo(Constants.NOT_BLANK);
		}
		return AjaxJson.getSuccessInfo(ticketService.get(id));
	}

	/**
	 * 筛选加载列表
	 * 
	 * @return
	 */
	@RequestMapping("page.do")
	@ResponseBody
	public AjaxJson page(Ticket ticket, Pager pager) {
		return AjaxJson.getSuccessInfo(ticketService.pageByEntity(ticket, pager));
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	@RequestMapping("add.do")
	@ResponseBody
	public AjaxJson add(Ticket ticket) {
		if (ticket.getSort() == null) {
			ticket.setSort(ticketService.getMaxSort());
		}
		ticketService.save(ticket);
		return AjaxJson.getSuccessInfo();
	}

	/**
	 * 修改
	 * 
	 * @param ticket
	 * @return
	 */
	@RequestMapping("update.do")
	@ResponseBody
	public AjaxJson update(Ticket ticket) {
		if (ticket.getId() == null) {
			return AjaxJson.getFailInfo(Constants.NOT_BLANK);
		}
		Ticket param = new Ticket();
		param.setName(ticket.getName());
		param.setIdNe(ticket.getId());
		Ticket tmp = ticketService.getByEntity(param);
		if (tmp != null) {
			return AjaxJson.getFailInfo("票据名已存在");
		}
		Ticket old = ticketService.get(ticket.getId());
		BeanUtils.copyNotNullProperties(ticket, old);
		old.setType(ticket.getType());
		ticketService.update(old);
		return AjaxJson.getSuccessInfo();
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("remove.do")
	@ResponseBody
	public AjaxJson remove(Integer id) {
		if (id == null) {
			return AjaxJson.getFailInfo(Constants.NOT_BLANK);
		}
		ticketService.remove(id);
		return AjaxJson.getSuccessInfo();
	}

}
